package org.edu.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.edu.bean.Role_user;
import org.edu.bean.User;
import org.edu.dao.UserDao;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;


@Service
public class UserDaoImpl implements UserDao{
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	
	@SuppressWarnings("unchecked")
	public User getUser(String systemNumber) {
		User user =null ;
		List<User> list = hibernateTemplate.find("from User u where u.systemNumber = ? ",systemNumber) ;
		if(list.size()==1){
			user = (User)list.get(0);
		}
		return user ;
	}


	public Integer createUser(User user) {
		return (Integer)hibernateTemplate.save(user);
	}


	public Integer userAddRole(Role_user roleUser) {
		return (Integer) hibernateTemplate.save(roleUser);
	}


	@SuppressWarnings("unchecked")
	public List<User> selectUsers(User user, Integer page) {
		String sql = "from User u " ;
		if(user.getWorkProvinceId() != -1){
			sql += " where u.workProvinceId = "+user.getWorkProvinceId();
			if(user.getWorkCityId() != -1){
				sql += " and u.workCityId = "+user.getWorkCityId();
				if(user.getWorkCountyId() != -1){
					sql += " and u.workCountyId = "+user.getWorkCountyId();
				}
			}
		}
		
		return (List<User>) hibernateTemplate.find(sql);
	}


	public void delUser(User user) {
		delRole_userByUserId(user.getId());
		hibernateTemplate.delete(user);
		
	}


	public void delRole_userByUserId(int userId) {
		List<Role_user> roleUsers = selectRoleUsers(userId);
		for (Role_user roleUser : roleUsers) {
			hibernateTemplate.delete(roleUser);
		}
	}


	@SuppressWarnings("unchecked")
	public List<Role_user> selectRoleUsers(int userId) {
		return (List<Role_user>) hibernateTemplate.find("from Role_user ru where ru.userId = "+userId);
	}


	public User selectUserById(int userId) {
		return (User)hibernateTemplate.find("from User u where u.id = "+userId).get(0);
	}


	public boolean updateUser(User user) {
		hibernateTemplate.update(user);
		return true;
	}


	@SuppressWarnings("unchecked")
	public List<Role_user> selectRoleUserByUsIdRoId(int userId, int roleId) {
		return (List<Role_user>)hibernateTemplate.find("from Role_user ru where roleId = "+roleId+" and userId = "+userId);
	}


	public void delRole_user(Role_user roleUser) {
		hibernateTemplate.delete(roleUser);
	}


	public Object findById(Class clazz, int Id) {
		List  ls=hibernateTemplate.find("from "+clazz.getSimpleName()+" obj where obj.id="+Id);
		Object obj=null;
		if (ls.size()>0)
		 obj=ls.get(0);
		return obj;
	}


	public Object findUsinfoByUserId(int Id) {
		List  ls=hibernateTemplate.find("from UserInfo  obj where obj.userId="+Id);
		Object obj=null;
		if (ls.size()>0)
		 obj=ls.get(0);
		return obj;
	}


	public Object saveOrUpdate(Object obj) {
		hibernateTemplate.saveOrUpdate(obj);
		return obj;
	}
	
}